import modelExtend from 'dva-model-extend'
import moment from 'moment'
import { routerRedux } from 'dva/router'
import { pageModel } from '@/common/model/BaseModel'
import {
  getCheckList,
  getCheckDetail,
  delPropCheck,
  updatePropCheck,
} from '../pages/property/propCheck/services/propCheck'
import { getPropSelectList } from '@/services/property'

export default modelExtend(pageModel, {
  namespace: 'propCheck',
  state: {
    //
    currentItem: {},
    selectedRowKeys: [],
    formValues: {},
    pd_status: [],
    //
    showProps: false,
    PropsData: [],
  },
  effects: {
    *fetchList({ payload }, { call, put }) {
      console.log('fetchCheckList')
      const res = yield call(getCheckList, payload || {})
      yield put({
        type: 'updateState',
        payload: {
          list: res.data.data,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: res.data.resourceSize,
            current: res.data.current,
            pageSize: res.data.size,
          },
          formValues: payload,
        },
      })
    },

    *fetchSelectPropData({ payload }, { call, put }) {
      const res = yield call(getPropSelectList, payload)
      yield put({
        type: 'updateState',
        payload: {
          [payload.type]: res.data,
        },
      })
    },

    *fetchProps({ payload }, { call, put }) {
      const res = yield call(getCheckDetail, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            PropsData: res.data.zcList,
          },
        })
      }
    },

    *remove({ payload }, { call, put, select }) {
      const res = yield call(delPropCheck, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            selectedRowKeys: [],
          },
        })
        const formValues = yield select(state => state.formValues)
        yield put({
          type: 'fetchList',
          payload: {
            current: 1,
            ...formValues,
          },
        })
      }
    },

    *edit({ payload }, { call, put }) {
      //
      const res = yield call(getCheckDetail, payload)
      if (res && res.success) {
        const DATA = res.data
        const rangeValue = [
          moment(DATA.pdStartDate, 'YYYY-MM-DD'),
          moment(DATA.pdEndDate, 'YYYY-MM-DD'),
        ]

        const currentItemData = {
          ...DATA,
          rangeValue,
          scope: DATA.scope ? DATA.scope.split(',') : [],
          status: DATA.status ? DATA.status.split(',') : [],
          zcClass: DATA.zcClass ? DATA.zcClass.split(',') : [],
          zcSeat: DATA.zcSeat ? DATA.zcSeat.split(',') : [],
        }

        yield put({
          type: 'updateState',
          payload: {
            currentItem: currentItemData,
          },
        })
      }
    },

    *save({ payload }, { call, put }) {
      const res = yield call(updatePropCheck, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            currentItem: {},
          },
        })
        yield put(
          routerRedux.push({
            pathname: '/property/check',
          }),
        )
      }
    },
  },
})
